package section_08.simulation.restaurantSimulation;

import java.util.concurrent.SynchronousQueue;

public class Customer implements Runnable {
	private static int counter = 0;
	private final int id = counter++;
	private final WaitPerson waitPerson;
	private SynchronousQueue<Plate> placeSetting = new SynchronousQueue<>();

	public Customer(WaitPerson w) {
		waitPerson = w;
	}

	public void deliver(Plate p) throws InterruptedException {
		placeSetting.put(p);
	}

	public void run() {
		while (true) {
			Food food = Course.randSelection();
			try {
				waitPerson.placeOrder(this, food);
				System.out.println(this + "eating " + placeSetting.take());
			} catch (InterruptedException e) {
				System.out.println(this + "waiting for " + food.toString() + " interrupted");
				break;
			}
		}
		System.out.println(this + "finished meal, leaving");
	}

	public String toString() {
		return "Customer " + id + " ";
	}

}
